package JDBC.util;

import 算法.冒泡排序;

import java.sql.*;

public class JDBCUtil {
private static Connection connection=null;
//      连接工具类
//    工具类必须要有静态方法，私有的变量
//    配置的内容需要再静态方法内部填写
    public JDBCUtil(){}
    static {
        String url="jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf8&useSSL=false";
        String password="123456";
        String user="root";
        try {
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("数据库驱动加载成功！");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        try {
             connection= DriverManager.getConnection
                    (url,user,password);
            System.out.println("数据库连接成功！");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public static Connection GetConnection(){
        return connection;
    }
//    释放资源的方法
    public static void close(Connection conn, Statement state, ResultSet rS) {
        if (conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }

        if (state!=null){
            try {
                state.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }

        if (rS!=null){
            try {
                rS.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
